package com.wechart.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by cxj on 2017/7/10.
 */
public class ImportExcel implements Import {


    private int sheetNum = 0;
    private int rowNum = 0;
    private InputStream inputStream;
    private HSSFWorkbook workbook;

    public ImportExcel(InputStream is) throws IOException {
        this.inputStream = is;
        this.workbook = new HSSFWorkbook(inputStream);
    }

    @SuppressWarnings("deprecation")
    public String[] readLine() {
        if (sheetNum < workbook.getNumberOfSheets()) {
            HSSFSheet sheet = workbook.getSheetAt(sheetNum);
            // getLastRowNum索引从0开始
            // int rowLength = sheet.getLastRowNum();
            int physicalRowLength = sheet.getPhysicalNumberOfRows();
            if (rowNum < physicalRowLength) {
                HSSFRow row = sheet.getRow(rowNum);
                // 空行代表结束
                if (row == null) return null;
                // 表格有空列
                int cellLength = row.getLastCellNum();
                String[] rowData = new String[cellLength];
                for (int i = 0; i < cellLength; i++) {
                    HSSFCell cell = row.getCell((short) i);
                    if (cell != null) {
                        // 根据单元格格式分别取值
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
                            rowData[i] = (long) cell.getErrorCellValue() + "";
                        }
                        else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                            // 判断是否是整型或浮点型
                            double a = cell.getNumericCellValue();
                            double eq = (long) a - a;
                            if (eq == 0.0) {
                                rowData[i] = (long) cell.getNumericCellValue() + "";
                            }
                            else {
                                rowData[i] = cell.getNumericCellValue() + "";
                            }
                        }
                        else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                            rowData[i] = cell.getCellFormula();
                        }
                        else {
                            rowData[i] = cell.getStringCellValue();
                        }
                    }
                    else {
                        rowData[i] = "";
                    }

                    // 去除两端空白符
//                    rowData[i] = StringUtils.null2String(rowData[i]);
                }
                // 换页
                if (rowNum == physicalRowLength - 1) {
                    sheetNum++;
                    rowNum = 0;
                }
                else {
                    rowNum++;
                }
                return rowData;
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }

    public static void main(String[] args) {
        File file = new File("d:\\test.xls");
        Integer number = 0;
        try {
            String[] s = null;
            ImportExcel ee = new ImportExcel(new FileInputStream(file));
            while ((s = ee.readLine()) != null) {
                System.out.println("【" + ++number + "】" + StringUtils.ArrayToString(s, ","));
            }
            // HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
            // HSSFSheet sheet = workbook.getSheetAt(0);
            // HSSFRow row = sheet.getRow(0);
            // HSSFCell cell = row.getCell((short)(row.getLastCellNum()-1));
            // System.out.println(cell.getCellType());
            // System.out.println(row.getLastCellNum() + "," + cell.getNumericCellValue() *
            // 100/100);
            // row = sheet.getRow(1);
            // System.out.println(row.getLastCellNum() + "," +
            // row.getCell((short)(row.getLastCellNum()-1)));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
